package junyi.card;

import junyi.classic.functions.Dodge;

import java.util.ArrayList;
import java.util.List;

/**
 * User: Junyi BEI
 * Date: 12-9-30
 * Time: 下午7:13
 */
public class CardFilter {
  public final static int GREATER_THAN = 2;
  public final static int EQUALS_OR_GREATER_THAN = 1;
  public final static int EQUALS = 0;
  public final static int EQUALS_OR_LESS_THAN = -1;
  public final static int LESS_THAN = -2;
  private List<FilterSetting> filterSettings;

  public static CardFilter createCardFilter(int d, CardProperty p) {
    CardFilter cardFilter = new CardFilter();
    cardFilter.addProperty(d, p);
    return  cardFilter;
  }

  public int addProperty(int d, CardProperty p) {
    FilterSetting filterSetting = new FilterSetting(d, p);
    if(filterSettings == null) {
      filterSettings = new ArrayList<FilterSetting>();
    }
    filterSettings.add(filterSetting);
    return 0;
  }

  public List<Card> doFilter(List<Card> cs) {
    List<Card> resultCards = new ArrayList<Card>();
    if(filterSettings == null) {
      cs.addAll(cs);
      return resultCards;
    }
    for(Card c : cs) {
      boolean qualified = true;
      for(FilterSetting fs : filterSettings) {
        CardProperty property = fs.property;
        int direction = fs.direction;
        if(direction == GREATER_THAN) {
          if(property.greaterThan(c) == CardProperty.SUCCESS) {
            continue;
          }
          qualified = false;
          break;
        }
        if(direction == EQUALS_OR_GREATER_THAN) {
          if(property.equalsTo(c) == CardProperty.SUCCESS || property.greaterThan(c) == CardProperty.SUCCESS) {
            continue;
          }
          qualified = false;
          break;
        }
        if(direction == EQUALS) {
          if(property.equalsTo(c) == CardProperty.SUCCESS) {
            continue;
          }
          qualified = false;
          break;
        }
        if(direction == EQUALS_OR_LESS_THAN) {
          if(property.equalsTo(c) == CardProperty.SUCCESS || property.lessThan(c) == CardProperty.SUCCESS) {
            continue;
          }
          qualified = false;
          break;
        }
        if(direction == LESS_THAN) {
          if(property.lessThan(c) == CardProperty.SUCCESS) {
            continue;
          }
          qualified = false;
          break;
        }
      }
      if(qualified) {
        resultCards.add(c);
      }
    }
    return resultCards;
  }

  private class FilterSetting {
    private int direction;
    private CardProperty property;

    private FilterSetting(int d, CardProperty p) {
      this.direction = d;
      this.property = p;
    }
  }
}
